package cn.edu.nju.software.android.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import me.maxwin.view.IListviewCallback;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import cn.edu.nju.software.android.adapter.MessageListViewAdapter;
import cn.edu.nju.software.android.adapter.StatusListViewAdapter;
import cn.edu.nju.software.android.service.MessagesService;
import android.os.Bundle;
import android.os.Handler;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;

public class MessagesActivity extends Activity implements
IXListViewListener, IListviewCallback{
	private XListView listView;
	private StatusListViewAdapter atMeStatusListViewAdapter;
	private MessageListViewAdapter atMeCommentListViewAdapter;
	private MessageListViewAdapter commentMeListViewAdapter;
	private String type;
	private Handler mHandler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_messages);

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);

		type = getIntent().getExtras().getString("type");
		listView = (XListView)findViewById(R.id.message_list);
		if (type.equals("mention_status")) {
			MessagesService.mention_status_unread = 0;
			atMeStatusListViewAdapter = new StatusListViewAdapter(this, this);
			listView.setAdapter(atMeStatusListViewAdapter);
		} else if (type.equals("mention_cmt")) {
			MessagesService.mention_cmt_unread = 0;
			atMeCommentListViewAdapter = new MessageListViewAdapter(this, this, MessageListViewAdapter.AT_ME);
			listView.setAdapter(atMeCommentListViewAdapter);
		} else if (type.equals("cmt")){
			MessagesService.cmt_unread = 0;
			commentMeListViewAdapter = new MessageListViewAdapter(this, this, MessageListViewAdapter.COMMENT_ME);
			listView.setAdapter(commentMeListViewAdapter);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.messages, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case android.R.id.home:
			finish();
			break;
		default:
			return true;
		}
		return false;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public void onComplete() {
		Date timeNow = new Date();
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		String time = format.format(timeNow);
		listView.stopRefresh();
		listView.stopLoadMore();
		listView.setRefreshTime(time);
	}

	@Override
	public void disableLoadFunction() {
		listView.setPullLoadEnable(false);

	}

	@Override
	public void enableLoadFunction() {
		listView.setPullLoadEnable(true);		
	}

	@Override
	public void resetToTopRow() {

	}

	@Override
	public void onRefresh() {
		if (type.equals("mention_status")) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					atMeStatusListViewAdapter.getNewestStatuses();
				}
			}, 2000);
		} else if (type.equals("mention_cmt")) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					atMeCommentListViewAdapter.getNewestComments();
				}
			}, 2000);
		} else if (type.equals("cmt")){
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					commentMeListViewAdapter.getNewestComments();
				}
			}, 2000);
		}
	}

	@Override
	public void onLoadMore() {
		if (type.equals("mention_status")) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					atMeStatusListViewAdapter.loadMoreStatuses();
				}
			}, 2000);
		} else if (type.equals("mention_cmt")) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					atMeCommentListViewAdapter.getMoreComments();
				}
			}, 2000);
		} else if (type.equals("cmt")){
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					commentMeListViewAdapter.getMoreComments();
				}
			}, 2000);
		}
	}

}
